<div id="kaoHeYixianyuangong" style="height: 100%;width: 100%">
    <div style="height:93%">
        <!--奖金表格-->
        <table id="baoZhuangCheJianYiXianYuanGong"></table>
    </div>
    <div id="baoZhuangCheJianYiXianYuanGongToolbar" style="display: none;">
        <div style="width: 550px;">
            <a class="easyui-linkbutton" data-options="toggle:true,group:'g2',plain:true">组别:</a>
            <input id="kaoHeYixianyuangongFormEmpGroup" class= "easyui-combobox" data-options="valueField:'empCode',textField:'empName',width:100"  type= "text"  required ="required">
            <a class="easyui-linkbutton c4" data-options="plain:true" style="height: 90%" onClick="toLoadBzYixianyuangong()"><i class="fa fa-plus-square fa-fw" aria-hidden="true"></i>加载员工</a>
            <a class="easyui-linkbutton" data-options="plain:true">|</a>
            <a class="easyui-linkbutton c1" data-options="plain:true" style="height: 90%" onclick="javaScript:$('#baoZhuangCheJianYiXianYuanGong').edatagrid('saveRow')"><i class="fa fa-floppy-o fa-fw" aria-hidden="true"></i>保存</a>
            <a class="easyui-linkbutton" data-options="plain:true">|</a>
            <a class="easyui-linkbutton c6" data-options="plain:true" style="height: 90%" onClick="javaScript:$('#baoZhuangCheJianYiXianYuanGong').edatagrid('addRow')"><i class="fa fa-plus-square fa-fw" aria-hidden="true"></i>新增</a>
            <a class="easyui-linkbutton" data-options="plain:true">|</a>
            <a class="easyui-linkbutton c5" data-options="plain:true" style="height: 90%" onclick="javaScript:$('#baoZhuangCheJianYiXianYuanGong').edatagrid('destroyRow')"><i class="fa fa-trash fa-fw" aria-hidden="true"></i>删除</a>
        </div>
    </div>
    <form accept-charset=""></form>
    <script th:inline="none">
            //当前编辑行的索引
            var baoZhuangCheJianYiXianYuanGongIndex=0;

            //第一次进入页面
            yiXianYuanGongInit()
            //初始化组长按钮事件
            $("#kaoHeYixianyuangongFormEmpGroup").combobox({
                url:"/jx/bzcj/selectBZCJAllGroupLeader?pkDept="+ bzcj_currentDept+"&postId="+JX_BZCJ_ZUZHANG_CODE
            })

            //加载员工
            function toLoadBzYixianyuangong(){
                $("#baoZhuangCheJianYiXianYuanGong").datagrid("loadData",[])
                //获取组别
                var zuZhangId = $("#kaoHeYixianyuangongFormEmpGroup").combobox("getValue");
                var pstName = $("#kaoHeYixianyuangongFormEmpGroup").combobox("getText");
                if (!bzcj_currentMonth || !bzcj_currentYear){
                    layer.msg("请先选择日期！");
                    return
                }
                if (!zuZhangId){
                    layer.msg("请选择班组！");
                    return
                }
                //添加数据
                addBZCJYiXianYuanGongData(bzcj_currentYear,bzcj_currentMonth,zuZhangId,pstName)
            }


            function bzYiXianYuanGongColumns() {
                var currentYM;
                if (!bzcj_currentMonth || !bzcj_currentYear){
                    currentYM=""
                }else {
                    currentYM= bzcj_currentYear+"年"+bzcj_currentMonth+"月";
                }

                return [//表头
                    [
                        {"title": ' <span style="font-size: 18px">一线员工综合绩效奖金申请表</span><span style="color: #009E94">（效率奖金 +损耗率奖金+规范化奖金+质量奖金）*出勤率+发现异常奖金-违规扣款</span>',"align": "center", "colspan":21},

                    ],
                    [  {"title": '考核部门（科室）:',"align": "center", "colspan":3},
                        {"field": 'id3', "title": "生产部/包装车间","align": "center", "colspan":2},
                        {"title": '', "align": "center","colspan":11},
                        {"title": '考核周期:',"align": "center","colspan":3},
                        {"field": 'id2', "title": currentYM, "align": "center","colspan":2},
                    ],
                    [
                        {"field": 'id', "title": 'ID', "width": 0, "align": "center", "hidden": "true"},
                        {"field": 'empName', "title": '姓名', "width": 30, "align": "center"},
                        {"field": 'empCode', "title": '工号', "width": 30, "align": "center"},
                        {"field": 'zuZhangName', "title": '所属组长', "width": 30, "align": "center"},
                        {"field": 'postName', "title": '岗位名称', "width": 30, "align": "center","hidden": "true"},
                        {"field": 'assessType', "title": '绩效类型', "width": 30, "align": "center","hidden": "true"},
                        {"field": 'teamEfficiency',"title": '产线生产<br/>效率(%)',"width": 30,"align": "center",
                            "editor": {"type": "textbox", "options": {"onChange":baoZhuangCheJianYiXianYuanGongProductivity}}},
                        {"field": 'performanceBonusNorm', "title": '绩效奖金<br/>标准', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                        {"field": 'efficiencyBonus', "title": '效率奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangCheJianYiXianYuanGong}}},
                        {"field": 'lossRate', "title": '损耗率', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":baoZhuangCheJianYiXianYuanGongLossRate}}},
                        {"field": 'lossBonus', "title": '损耗率<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangCheJianYiXianYuanGong}}},
                        {"field": 'score5s', "title": '5s得分', "width": 30,"align": "center","editor": {"type": "textbox", "options":  {"onChange":baoZhuangCheJianYiXianYuanGongScore5s}}},
                        {"field": 'penalty5s', "title": '规范化<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangCheJianYiXianYuanGong}}},
                        {"field": 'penaltyDayRate', "title": '质量天数<br/>比率(%)', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":baoZhuangCheJianYiXianYuanGongPenaltyDayRate}}},
                        {"field": 'penaltyBonusNorm', "title": '质量奖金<br/>标准', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":baoZhuangCheJianYiXianYuanGongPenaltyBonusNorm}}},
                        {"field": 'penaltyBonus', "title": '质量奖金', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangCheJianYiXianYuanGong}}},
                        {"field": 'attendanceRate', "title": '出勤率<br/>(%)', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangCheJianYiXianYuanGong}}},
                        {"field": 'exceptionBonus', "title": '发现异常<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangCheJianYiXianYuanGong}}},
                        {"field": 'illegalFine', "title": '违规扣款', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangCheJianYiXianYuanGong}}},
                        {"field": 'resultMoney', "title": '综合<br/>绩效（元）', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                        {"field": 'remarks', "title": '备注', "width": 130,"align": "center","editor": {"type":"textarea"}},
                    ]]
            }

            //绩效考核表初始化方法
            function yiXianYuanGongInit(data) {

                //奖金基数表格
                $("#baoZhuangCheJianYiXianYuanGong").edatagrid({
                    nowrap: false,
                    columns: bzYiXianYuanGongColumns(),
                    data:data,
                    showFooter: true,
                    autoSave:true,
                    fitColumns: true,
                    //scrollbarSize:0,  //最右侧多出来的去除
                    toolbar:"#baoZhuangCheJianYiXianYuanGongToolbar",
                    onBeforeEdit:function (index, row) { //编辑器打开触发事件
                        baoZhuangCheJianYiXianYuanGongIndex=index;//保存当前索引

                    },
                    onAfterEdit:function (index, row) {
                       // console.log("在用户完成编辑一行的时候触发:合计")
                        loadFooter();
                    },
                    onDestroy:function (index,row) { //删除一行触发事件
                        loadFooter();
                    }

                })
                loadFooter();
            }

            //产线生产效率触发事件:计算绩效奖金标准
            function baoZhuangCheJianYiXianYuanGongProductivity(newValue,oldValue) {
                if(newValue){
                    //赋值绩效奖金标准基数
                    var rEfficiencyBonus=0;
                    //若当月包装车间生产效率＜98%时，则当月质量奖基数为300元
                    var penaltyBonusNorm=300;
                    if (newValue>=98 && newValue<100){
                        rEfficiencyBonus=500;
                        penaltyBonusNorm=500;

                    }else if (newValue>=100 && newValue<105 ){
                        rEfficiencyBonus=600;
                        penaltyBonusNorm=600;
                    }else if (newValue>=105){
                        rEfficiencyBonus=700;
                        penaltyBonusNorm=700;
                    }
                    //赋值绩效奖金标准
                    getEditObject("performanceBonusNorm").textbox("setValue",rEfficiencyBonus);
                    //赋值效率奖金
                    getEditObject("efficiencyBonus").textbox("setValue",rEfficiencyBonus*0.45);
                    //赋值质量奖金标准
                    getEditObject("penaltyBonusNorm").textbox("setValue",penaltyBonusNorm);
                }

            }

            //损耗率触发事件：计算损耗奖金
            function baoZhuangCheJianYiXianYuanGongLossRate(newValue,oldValue){
                console.log("损耗率触发事件：计算损耗奖金")
                newValue=newValue?Number(newValue):0;
                var result=0;
                var performanceBonusNorm=Number(getEditObject("performanceBonusNorm").textbox("getValue")); //绩效奖金标准
                //损耗率 小于99：绩效奖金0；(包括)99~100之间：绩效奖金*0.05;等于100：绩效奖金*0.05，
                if (newValue==100){
                    result=performanceBonusNorm*0.05;
                }
                if (newValue>=99 && newValue<100){
                    result=performanceBonusNorm*0.03;
                }
                getEditObject("lossBonus").textbox("setValue",fomatFloat(result,2));  //赋值损耗率奖金
            }

            //5s得分触发事件：计算规范化奖金
            function baoZhuangCheJianYiXianYuanGongScore5s(newValue,oldValue){
                var result=0;
                newValue=newValue?Number(newValue):0;
                //大于或等于85：绩效奖金标准*0.05
                if (newValue>=85){
                    var performanceBonusNorm=Number(getEditObject("performanceBonusNorm").textbox("getValue")) //绩效奖金标准
                    result=performanceBonusNorm*0.05
                }
                var penalty5s= getEditObject("penalty5s");   //规范化奖金
                penalty5s.textbox("setValue",fomatFloat(result,2));
            }

            //质量天数比率(%)触发事件：计算质量奖金
            function baoZhuangCheJianYiXianYuanGongPenaltyDayRate(newValue,oldValue){
                //调用质量奖金标准事件
                baoZhuangCheJianYiXianYuanGongPenaltyBonusNorm(getEditObject("penaltyBonusNorm").textbox("getValue"));
            }

            //质量奖金标准触发事件：计算质量奖金
            function baoZhuangCheJianYiXianYuanGongPenaltyBonusNorm(newValue,oldValue){
                newValue=newValue?Number(newValue):0;
                var penalty5s= getEditObject("penaltyDayRate").textbox("getValue") //质量天数比率
                penalty5s=penalty5s?Number(penalty5s)/100:0;
                var penaltyBonus=getEditObject("penaltyBonus")   //质量奖金
                penaltyBonus.textbox("setValue",fomatFloat((penalty5s*newValue*0.45),2))

            }

            //合计综合绩效
            function totalBaoZhuangCheJianYiXianYuanGong(newValue,oldValue) {
                //初始效率高点: 注意：js的0相等于空字符串和false，js的1相等于字符串"1"、true;0==false==""   1==true="1" 所以不要去判断了
                if (newValue!= undefined || newValue!= null){
                    var efficiencyBonus=getEditObject("efficiencyBonus").textbox("getValue"); //效率奖金
                    efficiencyBonus=efficiencyBonus?Number(efficiencyBonus):0;
                    var lossBonus=getEditObject("lossBonus").textbox("getValue");      //损耗奖金
                    lossBonus=lossBonus?Number(lossBonus):0;
                    var penalty5s=getEditObject("penalty5s").textbox("getValue");       //规范奖金
                    penalty5s=penalty5s?Number(penalty5s):0;
                    var penaltyBonus=getEditObject("penaltyBonus").textbox("getValue");//质量奖金
                    penaltyBonus=penaltyBonus?Number(penaltyBonus):0;
                    var attendanceRate=getEditObject("attendanceRate").textbox("getValue"); //出勤率
                    attendanceRate=attendanceRate?Number(attendanceRate)/100:1;
                    var exceptionBonus=getEditObject("exceptionBonus").textbox("getValue"); //发现异常奖金
                    exceptionBonus=exceptionBonus?Number(exceptionBonus):0;
                    var illegalFine=getEditObject("illegalFine").textbox("getValue");      //违规扣款
                    illegalFine=illegalFine?Number(illegalFine):0;
                    var resultMoney=getEditObject("resultMoney");       //绩效奖金
                    resultMoney.textbox("setValue",fomatFloat((efficiencyBonus+lossBonus+penalty5s+penaltyBonus)*attendanceRate+exceptionBonus-illegalFine,2))
                }
            }


            //加载页脚行
            function loadFooter(){
                var resultMoney=0;
                var rows = $("#baoZhuangCheJianYiXianYuanGong").datagrid("getRows");
                if (rows){
                    rows.forEach(function (val) {
                        resultMoney+= Number(val.resultMoney);
                    })
                }
                resultMoney=fomatFloat(resultMoney,2);
                $("#baoZhuangCheJianYiXianYuanGong").datagrid("reloadFooter",[
                    {"empCode":"合计","resultMoney":resultMoney},
                ])
            }

            //获取当前编辑行指定的jquery对象
            function getEditObject(fieldValue) {
                var j = $('#baoZhuangCheJianYiXianYuanGong').datagrid('getEditor', {
                    index: baoZhuangCheJianYiXianYuanGongIndex,
                    field: fieldValue
                }).target; //编辑器对象
                return j
            }

            function addBZCJYiXianYuanGongData(year,month,zuZhangId,pstName){
                //根据组长的id获取组长下的员工信息
                var datas=commonAjax("post","/jx/bzcj/selectJxAssessBzcj",{assessType:1,postName:pstName,year:year,month:month,empGroup:zuZhangId,pkDept:bzcj_currentDept})
                //重新初始化表
                yiXianYuanGongInit(datas);
            }

    </script>
</div>